package view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import dao.DBUtil;
import dao.StudentDao;
import entity.Student_Info;

public class Student_Info_Panel extends JPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] tf = new JTextField[3];
	private JButton[] but = new JButton[4];
	private DefaultTableModel dtm;
	private JButton[] crossBut = new JButton[2];
	private JLabel infolab;
    private StudentDao studao;
	
	public Student_Info_Panel() {
		// TODO Auto-generated constructor stub
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);

		Font font = new Font("宋体", Font.BOLD, 20);
		Font font1 = new Font("宋体", Font.BOLD, 15);
		Font font2 = new Font("宋体", Font.BOLD, 13);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;

		for (int i = 0; i < 10; i++) {
			gbc.gridx = 0;
			gbc.gridy = i;
			gbc.weighty = 0;
			gbc.weightx = 0;
			JLabel lab = new JLabel();
			gbc.insets = new Insets(5, 0, 5, 0);
			layout.setConstraints(lab, gbc);
			add(lab);
		}

		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridheight = 0;
		gbc.weighty = 0;
		gbc.weightx = 0.3;
		dtm = new DefaultTableModel(new Object[][] {}, new String[] { "ID", "姓名", "性别" });
		JTable table = new JTable(dtm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane sp = new JScrollPane(table);
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(sp, gbc);
		add(sp);

		for (int i = 0; i < 4; i++) {
			gbc.gridx = 2 + i;
			gbc.gridy = 0;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weightx = 0;
			gbc.weighty = 0;
			JLabel lab1 = new JLabel();
			gbc.insets = new Insets(5, 5, 5, 5);
			layout.setConstraints(lab1, gbc);
			add(lab1);
		}

		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 1;
		gbc.weightx = 0;
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		infolab = new JLabel(DBUtil.getMessage());
		infolab.setFont(font);
		infolab.setHorizontalAlignment(JLabel.CENTER);
		panel.add(infolab, BorderLayout.CENTER);
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(panel, gbc);
		add(panel);

		String[] labText = { "学号", "姓名", "性别" };
		for (int i = 0; i < labText.length; i++) {
			gbc.gridx = 2;
			gbc.gridy = 2 + i;
			gbc.gridheight = 1;
			gbc.weighty = 0;
			gbc.weightx = 0;
			JLabel labName1 = new JLabel(labText[i]);
			labName1.setFont(font1);
			gbc.insets = new Insets(20, 5, 0, 20);
			layout.setConstraints(labName1, gbc);
			add(labName1);

			gbc.gridx = 3;
			gbc.gridy = 2 + i;
			gbc.gridwidth = 0;
			gbc.weighty = 0;
			gbc.weightx = 0;
			tf[i] = new JTextField(20);
			gbc.insets = new Insets(20, 0, 5, 20);
			layout.setConstraints(tf[i], gbc);
			add(tf[i]);
		}

		gbc.gridx = 2;
		gbc.gridy = 5;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 0;
		gbc.weightx = 0;
		JPanel panel1 = new JPanel();
		gbc.insets = new Insets(20, 5, 5, 20);
		layout.setConstraints(panel1, gbc);
		add(panel1);

		String[] crossButName = { "上一条", "下一条" };
		String[] butName = { "查询", "添加", "修改", "删除" };
		for (int i = 0; i < crossBut.length; i++) {
			gbc.gridx = 3 + i;
			gbc.gridy = 6;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weighty = 0;
			gbc.weightx = 0;
			crossBut[i] = new JButton(crossButName[i]);
			crossBut[i].setFont(font2);
			gbc.insets = new Insets(20, 5, 5, 20);
			layout.setConstraints(crossBut[i], gbc);
			add(crossBut[i]);
		}

		for (int i = 0; i < butName.length; i++) {
			gbc.gridx = 2 + i;
			gbc.gridy = 7;
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.weightx = 0;
			gbc.weighty = 0;
			but[i] = new JButton(butName[i]);
			but[i].setFont(font2);
			gbc.insets = new Insets(20, 5, 5, 20);
			layout.setConstraints(but[i], gbc);
			but[i].addActionListener(this);
			add(but[i]);
		}

		gbc.gridx = 2;
		gbc.gridy = 8;
		gbc.gridwidth = 0;
		gbc.gridheight = 1;
		gbc.weighty = 1;
		gbc.weightx = 0;
		JPanel panel2 = new JPanel();
		gbc.insets = new Insets(5, 5, 5, 5);
		layout.setConstraints(panel2, gbc);
		add(panel2);

		studao = new StudentDao();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == but[0]) {
			if (tf[0].getText().trim().equals("")) {
				while (dtm.getRowCount() != 0) {
					dtm.removeRow(0);
				}
				List<Student_Info> stu = studao.select();
				for (Student_Info stuinfo : stu) {
					dtm.addRow(
							new Object[] { String.valueOf(stuinfo.getId()), stuinfo.getName(), stuinfo.getGender() });
				}
				infolab.setText("查询完成！");
				tf[0].setText("");
				tf[1].setText("");
				tf[2].setText("");
			}else{
				while (dtm.getRowCount() != 0) {
					dtm.removeRow(0);
				}
				Student_Info stu1=studao.select(Integer.valueOf(tf[0].getText().trim()));
				dtm.addRow(new Object[]{String.valueOf(stu1.getId()),stu1.getName(),stu1.getGender()});
				tf[1].setText(stu1.getName());
				tf[2].setText(stu1.getGender());
				infolab.setText("查询完成！");
			}
		}

		if (e.getSource() == but[1]) {
			if(tf[0].getText().trim().equals("")||tf[1].getText().trim().equals("")||tf[2].getText().trim().equals("")) return;
			List<Student_Info> stu=studao.select();
			for (Student_Info stuinfo : stu)  if(stuinfo.getId()==Integer.valueOf(tf[0].getText().trim())) return;
			Student_Info stuinfo = new Student_Info();
			stuinfo.setId(Integer.valueOf(tf[0].getText()));
			stuinfo.setName(tf[1].getText());
			stuinfo.setGender(tf[2].getText());
			studao.insert(stuinfo);
			while (dtm.getRowCount() != 0) {
				dtm.removeRow(0);
			}
			dtm.addRow(new Object[] { tf[0].getText(), tf[1].getText(), tf[2].getText() });
			infolab.setText("添加成功！");
			tf[0].setText("");
			tf[1].setText("");
			tf[2].setText("");
		}

		if (e.getActionCommand() == "修改") {

		}

		if (e.getActionCommand() == "删除") {
			int id = Integer.valueOf(tf[0].getText());
			studao.delete(id);

		}
	}
}
